# coding=UTF-8
'''
Created on 25 Mar 2012

@author: R
'''

import re
from threading import Thread
from Queue import Queue
import estateGeneralinfo

#TODO buffer before write to file
infile = "D:\proj\Py\soufun\listURLs_utf.txt"
outfile = "D:\proj\Py\soufun\estate.csv"
fh_getl = open(infile,"r")
fh_putd = open(outfile,"a")

q = Queue()
MultiNumber = 2


def getinfo(wholeline):
    strg = re.compile("^(.*?),").search(wholeline).group(1)+","
    url_m = re.compile(",(.*?)$").search(wholeline).group(1)
    (url_d, url_php) = estateGeneralinfo.get_urls_from_portal(url_m)
    if url_d != "0" and url_php != "0":
        strg = strg + url_d+","
        strg = strg + estateGeneralinfo.resolve_phppage(url_php)
        strg = strg + estateGeneralinfo.resolve_detailpage(url_d)
    fh_putd.write(strg+"\n")
    print "done"+url_d
        
def working():
    while True:
        line = q.get()
        getinfo(line)
        q.task_done()

for i in range(MultiNumber):
    t = Thread(target=working)
    t.setDaemon(True)
    t.start()

for lines_in_links in fh_getl:
    q.put(lines_in_links)

q.join()
